package com.apes.fn.scm.commodityInquiry.model;

import com.apes.crm.business.model.InsuranceCompany;
import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.ServeType;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商品询价单
 * @autor yinyun
 * @Date 2019.12.4
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = CommodityInquiryOrder.CONST_TABLE_NAME)
public class CommodityInquiryOrder extends Model {
    public static final String CONST_TABLE_NAME = "pur_commodity_inquiry_order";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "商品询价单", prefix = "1")
    @Comment("询价单号")
    private String id;

    @Column(name = "source_business_type", length = 64)
    @Comment("来源单据类型")
    private String sourceBusinessType;

    @Column(name = "source_name", length = 64)
    @Comment("来源单据名称")
    private String sourceName;

    @Column(name = "source_business_key", length = 64)
    @Comment("来源单据号码")
    private String sourceBusinessKey;

//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "organization_id", referencedColumnName = "id")
//    @Comment("采购组织编码")
//    private Organization organizationId;
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "sale_organization_id", referencedColumnName = "id")
//    @Comment("销售组织编码")
//    private Organization saleOrganizationId;

    @Column(name = "original_source_business_type", length = 64)
    @Comment("原始来源单据类型")
    private String originalSourceBusinessType;

    @Column(name = "original_source_name", length = 64)
    @Comment("原始来源单据名称")
    private String originalSourceName;

    @Column(name = "original_source_business_key", length = 64)
    @Comment("原始来源单据号码")
    private String originalSourceBusinessKey;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "serve_type_id", referencedColumnName = "id")
    @Comment("服务类型编码")
    private ServeType serveTypeId;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司编码")
    private PartyRole companyId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "insurance_company_id", referencedColumnName = "id")
    @Comment("保险公司")
    private InsuranceCompany insuranceCompanyId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "persoonnel_id", referencedColumnName = "id")
    @Comment("人员编码")
    private PartyRole persoonnelId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "service_plan_id", referencedColumnName = "id")
    @Comment("服务制单人")
    private PartyRole servicePlan_id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @Comment("门店编码")
    private Dept deptId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "service_store_id", referencedColumnName = "id")
    @Comment("服务门店编码")
    private Dept serviceStoreId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "plate_number", referencedColumnName = "id")
    @Comment("车牌号码")
    private FnCustomerExpand plateNumber;

    @Column(name = "vehicle_code", length = 16)
    @Comment("车辆内码")
    private String vehicleCode;

    @Column(name = "matched_suppliers_valid", columnDefinition = "number(2) default 0")
    @Comment("已匹配供应商标记")
    private int matchedSuppliersValid;

    @Column(name = "MZZDBJ", columnDefinition = "number(2) default 0")
    @Comment("满足最低报价标记")
    private int MZZDBJ;

    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note;

    @Column(name = "base_note", length = 250)
    @Comment("基地备注")
    private String baseNote;

    @States(name = "单据状态",
    values = {@State(id="create", name="创建"), @State(id="review", name = "完成报价"), @State(id="sendBack", name="退回"),
            @State(id="inquiry", name="转询价"), @State(id="repurchase", name="转采购"), @State(id="delete", name="删除"),
            @State(id="stopQuote", name="终止报价"),
            @State(id="pushStore", name = "推门店")})
    @Column(name = "state", length = 10, nullable = false)
    @Comment("单据状态")
    private String state;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "creator_name", referencedColumnName = "id")
    @Comment("新增人")
    private PartyRole creatorName;

    @NotNull
    @Column(name = "create_date")
    @Comment("新增日期")
    private Date createDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "review_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole reviewUID;

    @Column(name = "review_date")
    @Comment("审核日期")
    private Date reviewDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "returned_person_id", referencedColumnName = "id")
    @Comment("退回人")
    private PartyRole returnedPersonId;

    @Column(name = "returned_date")
    @Comment("退回日期")
    private Date returnedDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "inquiry_id", referencedColumnName = "id")
    @Comment("转询价人")
    private PartyRole inquiryId;

    @Column(name = "inquiry_date")
    @Comment("转询价日期")
    private Date inquiryDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "repurchase_id", referencedColumnName = "id")
    @Comment("转采购人")
    private PartyRole repurchaseId;

    @Column(name = "repurchase_date")
    @Comment("转采购日期")
    private Date repurchaseDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "stop_quote_id", referencedColumnName = "id")
    @Comment("终止报价人")
    private PartyRole stopQuoteId;

    @Column(name = "stop_quote_date")
    @Comment("终止报价日期")
    private Date stopQuoteDate;

    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "push_store_uid", referencedColumnName = "id")
    @Comment("推门店人")
    private PartyRole pushStoreUid;

    @Column(name = "push_store_date")
    @Comment("推门店日期")
    private Date pushStoreDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_uid", referencedColumnName = "id")
    @Comment("删除人")
    private PartyRole deleteUid;

    @Column(name = "delete_date")
    @Comment("删除日期")
    private Date deleteDate;

    @NotNull
    @Column(name = "timestamp")
    @Comment("时间戳")
    private String timestamp;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "platform_id", referencedColumnName = "id")
    @Comment("平台")
    private PartyRole platform;

    @Column(name = "dabj", columnDefinition = "number(2) default 0")
    @Comment("带安标记")
    private int dabj;

    @Transient
    private String imageUrl; // 图片路径

    @Transient
    private PartyRole currentUser;

    @OneToMany(mappedBy = "commodityInquiryOrderId", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<CommodityInquiryItem> commodityInquiryItems = new ArrayList<>();

    public List<CommodityInquiryItem> getCommodityInquiryItems() {
        for (CommodityInquiryItem item:commodityInquiryItems) {
            item.setCommodityInquiryOrderId(this);
        }
        return commodityInquiryItems;
    }

    @Transient
    private String sourceBusinessTypeName;

    public String getSourceBusinessTypeName() {
        return getBusinessTypeName("sourceBusinessType");
    }


}
